<?php
/**
 * Created by PhpStorm.
 * User: Mysic
 * Date: 2020/12/23
 * Time: 17:38
 */

namespace app\common\mysic\profitShare\procedures;


use app\common\model\Wxsetting;
use app\common\mysic\exceptions\EntityErrorException;
use app\common\mysic\MysicBase;
use app\common\model\Order;

class ConditionJudgment extends MysicBase
{
    protected $whetherFinishOrderProfitSharing;
    protected $userUpgradable;
    protected $selfBonusAble;
    protected $otherBonusAble;
    protected $isVipCardOrder;

    public function __construct()
    {
        parent::__construct();
    }

    public function init(Order $userOrderEntity, Wxsetting $wxSettingEntity, ProfitShare $profitShare): bool
    {
        $this->whetherFinishOrderProfitSharing = false;
        $this->userUpgradable = true;
        $this->selfBonusAble = true;
        $this->otherBonusAble = true;
        $this->isVipCardOrder = false;
        if(!$userOrderEntity->getData()) {
            throw new EntityErrorException('用户消费订单实体不存在');
        }
        // 商户分红设置不存在
        if(!$wxSettingEntity) {
            $this->otherBonusAble = false;
            $this->userUpgradable = false;
            $this->selfBonusAble = false;
            return true;
        }
        // 订单为实物商品订单
        if($userOrderEntity->is_fictitious != 1 && $wxSettingEntity->real_reward == 0) {
            $this->whetherFinishOrderProfitSharing = true;
            $this->otherBonusAble = false;
            $this->userUpgradable = false;
            $this->selfBonusAble = false;
        }
        // 活动未开始
        if($wxSettingEntity->start_time > time()) {
            $this->otherBonusAble = false;
            $this->userUpgradable = false;
            $this->selfBonusAble = false;
        }
        // 活动已关闭
        if($wxSettingEntity->status != 1) {
            $this->userUpgradable = false;
            $this->selfBonusAble = false;
        }
        // 订单支付金额未达到微股东身份最低支付金额
        if($userOrderEntity->pay_price < $wxSettingEntity->money) {
            $this->userUpgradable = false;
            $this->selfBonusAble = false;
        }
        if(40 == $userOrderEntity->order_source) {
            $this->isVipCardOrder = true;
        }
        $profitAmount = 0;
        try {
            $profitAmount = $profitShare->calculateProfitAmount($userOrderEntity, $wxSettingEntity);
            $this->log->info($profitAmount, '计算分红总金额');
            RuntimeVariables::setProfitAmount($profitAmount);
        } catch (EntityErrorException $e) {
           $this->log->error(exceptionLogFormat($e), '条件判断逻辑跑出异常');
        }
        $profitUpperLimit = $profitShare->calculateProfitUpperLimit($userOrderEntity, $wxSettingEntity);
        $this->log->info($profitUpperLimit, '计算分红金额上限');
        RuntimeVariables::setProfitUpperLimit($profitUpperLimit);
        $receivesAmount = RuntimeVariables::getReceivesAmount();
        $this->log->info($receivesAmount,'初始化的参与分红总人数');
        // 不存在其他分红订单时，不需要给其他人分
        if($receivesAmount < 1) {
            $this->otherBonusAble = false;
        }
        //当笔支付订单分红,分红总人数+1
        if($this->selfBonusAble) {
            $receivesAmount++;
            RuntimeVariables::setReceivesAmount($receivesAmount);
        }
        // 不给当笔支付订单分红时，分红总人数需-1
        if($this->otherBonusAble && !$this->selfBonusAble) {
            $receivesAmount--;
            RuntimeVariables::setReceivesAmount($receivesAmount);
        }
        $this->log->info($receivesAmount,'计算后的参与分红总人数');
        $profit = $profitShare->calculateProfit($profitAmount, $profitUpperLimit, RuntimeVariables::getReceivesAmount());
        $this->log->info($profit, '计算平均分红金额');
        RuntimeVariables::setProfit($profit);
        if(bccomp($profit, '0',2) <= 0) {
            $this->otherBonusAble = false;
            $this->selfBonusAble = false;
        }
        $log = [
            '用户是否升级为微股东' => $this->userUpgradable,
            '是否给本次订单退款' => $this->selfBonusAble,
            '是否给其他微股东分红' => $this->otherBonusAble,
            '是否是储值卡订单' => $this->isVipCardOrder
        ];
        $this->log->info($log, '初始化条件判断结果');
        return true;
    }

    public function whetherFinishOrderProfitSharing(): bool
    {
        return $this->whetherFinishOrderProfitSharing;
    }

    public function userUpgradable(): bool
    {
        return $this->userUpgradable;
    }

    public function selfBonusAble(): bool
    {
        return $this->selfBonusAble;
    }

    public function otherBonusAble(): bool
    {
        return $this->otherBonusAble;
    }

    public function isVipCardOrder(): bool
    {
        return $this->isVipCardOrder;
    }

}